package cn.mall.order.utils;/**
 * @author shkstart
 * @create 2023-12-18 19:41
 */

import cn.mall.base.constants.BaseConstants;
import cn.mall.order.domain.Order;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Auther:Jarvis
 * @Date:2023年12月2023/12/18日19:41
 * @Description:
 */
public class  CommentLikeUtils {
    /**
     * 微博评论点赞数获取
     * @param order
     * @return
     * @throws Exception
     */
    public static String getCommentLike(Order order) throws Exception {
        // 获取共有多少页评论
        int totalPages = getPages(order);
        // 设置最小页数
        int minPage = 50;
        // 得到爬取页数
        int crawlPages = Math.min(minPage, totalPages);
        // 处理commentUrl
//        String[] splitCommentUrl = order.getCommentUrl().split("&");
        // 评论点赞数
        String likeCount = null;
        // 循环开始爬取
//        for (int i = 0; i < crawlPages; i++) {
////            System.out.println("正在爬取第" + i + 1 + "页");
////            String baseUrl = splitCommentUrl[0] + "&" + splitCommentUrl[1] + "&max_id=" + i + "&" + splitCommentUrl[2];
//            JSONObject json = getJson(baseUrl);
//            if (json == null) {
//                continue;
//            }
//            likeCount = jsonParse(json, order);
//            if (likeCount != null) {
////                System.out.println("点赞数：" + likeCount);
//                break;
//            }
//        }
        return likeCount;
    }

    /**
     * 获取评论的总页数
     *
     * @param order
     * @return
     * @throws IOException
     * @throws JSONException
     */
    public static int getPages(Order order) throws Exception {
        // 创建url
//        URL url = new URL(order.getCommentUrl());
//        // Http链接
//        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//        connection.setRequestMethod("GET");
//        connection.setRequestProperty("User-Agent", BaseConstants.Crawler.USER_AGENT);
//        connection.setRequestProperty("Cookie", BaseConstants.Crawler.COOKIE);
//        connection.connect();
//        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//        String line;
//        StringBuilder response = new StringBuilder();
//        while ((line = reader.readLine()) != null) {
//            response.append(line);
//        }
//        reader.close();
//        JSONObject jsonResponse = new JSONObject(response.toString());
//        int ok = jsonResponse.getInt("ok");
//        if (ok == 1) {
//            return jsonResponse.getJSONObject("data").getInt("max");
//        } else {
////            System.out.println("请求失败");
//            return 0;
//        }
        return 0;
    }

    /**
     * 爬取Json数据
     * @param url
     * @return
     * @throws IOException
     * @throws JSONException
     */
    public static JSONObject getJson(String url) throws Exception {
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", BaseConstants.Crawler.USER_AGENT);
        con.setRequestProperty("Cookie", BaseConstants.Crawler.COOKIE);
        con.connect();
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();
        return new JSONObject(response.toString());
    }

    /**
     * 拿去爬出来的Json数据中具体的信息
     * @param json
     * @return
     * @throws JSONException
     */
    public static String jsonParse(JSONObject json, Order order) throws JSONException {
        JSONArray data = json.getJSONObject("data").getJSONArray("data");
        String likeCount = null;
        for (int i = 0; i < data.length(); i++) {
            JSONObject item = data.getJSONObject(i);
            String text = item.getString("text");
            // 处理过后的微博评论
            String weiboComment = commentRegularExpression(text);
            // 处理后的用户评论
//            String userText = commentRegularExpression(order.getCommentText());
            String weiboUsername = item.getJSONObject("user").getString("screen_name");
//            if (weiboComment.equals(userText) && weiboUsername.equals(order.getWeiboUsername())) {
//                likeCount = item.getString("like_count");
//                break;
//            }
        }
        return likeCount;
    }

    /**
     * 评论正则表达式处理
     * @param text
     * @return
     */
    public static String commentRegularExpression(String text) {
        Pattern pattern = Pattern.compile("[^\\u4e00-\\u9fa5]");
        Matcher matcher = pattern.matcher(text);
        String comment = matcher.replaceAll("");
        return comment;
    }

}
